.TH std::atexit 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::atexit \- std::atexit

.SH Synopsis
   Defined in header <cstdlib>
   int atexit( /* c-atexit-handler */* func );                  \fI(until C++11)\fP
   int atexit( /* atexit-handler */* func );
   int atexit( /* c-atexit-handler */* func ) noexcept; \fB(1)\fP     \fI(since C++11)\fP
   int atexit( /* atexit-handler */* func ) noexcept;
   extern "C" using /* c-atexit-handler */ = void();        \fB(2)\fP (exposition only*)
   extern "C++" using /* atexit-handler */ = void();

   Registers the function pointed to by func to be called on normal program termination
   (via std::exit() or returning from the main function)

   The functions will be called during the destruction of the static
   objects, in reverse order: if A was registered before B, then the call
   to B is made before the call to A. Same applies to the ordering        \fI(until C++11)\fP
   between static object constructors and the calls to atexit: see
   std::exit.
   The functions may be called concurrently with the destruction of the
   objects with static storage duration and with each other, maintaining
   the guarantee that if registration of A was sequenced-before the       \fI(since C++11)\fP
   registration of B, then the call to B is sequenced-before the call to
   A, same applies to the sequencing between static object constructors
   and calls to atexit: see std::exit.

   The same function may be registered more than once.

   If a function exits via an exception, std::terminate is called.

   atexit is thread-safe: calling the function from several threads does not induce a
   data race.

   The implementation is guaranteed to support the registration of at least 32
   functions. The exact limit is implementation-defined.

.SH Parameters

   func - pointer to a function to be called on normal program termination

.SH Return value

   0 if the registration succeeds, nonzero value otherwise.

.SH Notes

   The two overloads are distinct because the types of the parameter func are distinct
   (language linkage is part of its type).

.SH Example


// Run this code

 #include <cstdlib>
 #include <iostream>

 void atexit_handler_1()
 {
     std::cout << "At exit #1\\n";
 }

 void atexit_handler_2()
 {
     std::cout << "At exit #2\\n";
 }

 int main()
 {
     const int result_1 = std::atexit(atexit_handler_1);
     const int result_2 = std::atexit(atexit_handler_2);

     if (result_1 || result_2)
     {
         std::cerr << "Registration failed!\\n";
         return EXIT_FAILURE;
     }

     std::cout << "Returning from main...\\n";
     return EXIT_SUCCESS;
 }

.SH Output:

 Returning from main...
 At exit #2
 At exit #1

.SH See also

   abort         causes abnormal program termination (without cleaning up)
                 \fI(function)\fP
   exit          causes normal program termination with cleaning up
                 \fI(function)\fP
   quick_exit    causes quick program termination without completely cleaning up
   \fI(C++11)\fP       \fI(function)\fP
   at_quick_exit registers a function to be called on std::quick_exit invocation
   \fI(C++11)\fP       \fI(function)\fP
   C documentation for
   atexit
